>>36+3\+9" />

【python】过长代码换行续行方法

您所在的位置:网站首页 python write换行 【python】过长代码换行续行方法

【python】过长代码换行续行方法

2024-01-09 09:57| 来源: 网络整理| 查看: 265

当需要进行计算的语句或输出的代码过长时,我们需要使用续行符进行上下行间的衔接。主要包括了\,(),"""string"""等方法。

1.反斜杠\

python中支持反斜杠\来进行续行,

#表达式续行 a = 1 + \ 2 print(a) >>> 3 6+3\ +9 >>> 18 # 输出续行 print("This is \ a long sentence ") >>> This is a long sentence print("a"+\ "b"+'c') >>> abc 2.括号()

可以用于容纳多行的表达式或者函数的输出入,括号内的数值可以被解析成一行:

# 括号包裹住分开在多行的表达式,实现续行 a = ( 1+ 2+ 3 ) print(a) >>> 6 # 作为函数的输入 a = 1 b = 2 c = 3 print( a, b, c ) >>> 1,2,3 #or: (1,2,3) # in py2.x # 这在某些复杂函数中也很常见,比如tensorflow中的reduce_max函数: tf.math.reduce_max( input_tensor, axis=None, keepdims=None, name=None, reduction_indices=None, keep_dims=None ) 3.使用块注释字符串

要输出很长的字符串需要换行时,可以利用块注释的方式来进行:

print(""" ______ _ / _____) | | | / ___ ___ | | | | / _ \ / _ \| | | \____| |_| | |_| | | \______)___/ \___/|_| """) >>> ______ _ / _____) | | | / ___ ___ | | | | / _ \ / _ \| | | \____| |_| | |_| | | \______)___/ \___/|_| ref: https://blog.csdn.net/NFR413/article/details/78386972 https://blog.csdn.net/baihhzm/article/details/73433160 https://blog.csdn.net/hllsmart/article/details/51236877 https://blog.csdn.net/codechelle/article/details/61199212 ascii art: https://zhuanlan.zhihu.com/p/27765087

在这里插入图片描述 pic from pexels.com



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3